﻿using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text;
using System.Security.Cryptography;

public partial class UserDefinedFunctions
{
    [SqlFunction]
    public static SqlString MD5([SqlFacet(IsNullable = true)] SqlString sData)
    {
        SqlString sOutPut = null;
        try
        {
            if (!sData.IsNull && sData.ToString() != "")
            {
                //byte bytHashedByte = new byte();
                UTF8Encoding uEncoder = new UTF8Encoding();
                MD5CryptoServiceProvider MD5Hasher = new MD5CryptoServiceProvider();

                byte[] bTemp = MD5Hasher.ComputeHash(uEncoder.GetBytes(sData.ToString()));
                sOutPut = Convert.ToBase64String(bTemp);
            }
            return (sOutPut);
        }
        catch (Exception)
        {
            throw new Exception("Failed to Encrypt");
        }
    }
};

